package com.bolt.admin.web.controller;

import com.bolt.admin.common.base.BaseController;
import com.bolt.admin.module.dict.dto.DictDTO;
import com.bolt.admin.module.dict.dto.DictDetailDTO;
import com.bolt.admin.module.dict.dto.converter.DictConverter;
import com.bolt.admin.module.dict.dto.converter.DictDetailConverter;
import com.bolt.admin.module.dict.entity.DictDetailEntity;
import com.bolt.admin.module.dict.entity.DictEntity;
import com.bolt.admin.module.dict.service.DictDetailService;
import com.bolt.admin.module.dict.service.DictService;
import com.bolt.convention.data.PageInfo;
import com.bolt.convention.data.ResultMessage;
import com.bolt.convention.data.Returns;
import com.bolt.core.view.components.Form;
import com.bolt.support.spring.jpa.specification.QueryHelp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Auto Generate Javadoc
 *
 * @author 日期:2020-10-23 17:16:08
 *         数据字典
 **/
@Controller
@RequestMapping("/admin/sys/dict")
public class DictController extends BaseController <DictEntity, Long> {

    @Autowired
    private DictService dictService;

    @Autowired
    private DictDetailService detailService;

    /**
     * 模块首页
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView index(Model model, HttpServletRequest request) {
        return super.index("/admin/sys/dict/index", request, model.asMap());
    }

    /**
     * 数据字典列表分页
     *
     * @return
     */
    @RequestMapping(value = "page", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage list(Pageable pageable, DictDetailDTO dto) {
        PageInfo <DictDetailEntity> pageInfo = detailService.findPage(pageable, (root, criteriaQuery, criteriaBuilder)
                -> QueryHelp.getPredicate(root, dto, criteriaBuilder));
        return Returns.success(pageInfo.map(K -> DictDetailConverter.toDTO(K)));

    }

    /**
     * 数据字典新增操作
     *
     * @return
     */
    @RequestMapping(value = "createDict", method = RequestMethod.GET)
    public String createDict() {
        return "/admin/sys/dict/input-dict-index";
    }

    /**
     * 数据字典新增操作
     *
     * @return
     */
    @RequestMapping(value = "createDictDetail", method = RequestMethod.GET)
    public String createDictDetail() {
        return "/admin/sys/dict/input-dictDetail-index";
    }

    /**
     * 数据字典编辑页
     *
     * @param id    菜单ID
     * @param model
     * @return
     */
    @RequestMapping(value = "editDict", method = RequestMethod.GET)
    public String editDict(@RequestParam("id") Long id, Model model) {
        DictEntity dictEntity = dictService.findOne(id).get();
        model.addAttribute(Form.PAGECONTEXT_FORMVALUES, DictConverter.toDTO(dictEntity));
        return "/admin/sys/dict/input-dict-index";
    }

    /**
     * 数据字典编辑页
     *
     * @param id    菜单ID
     * @param model
     * @return
     */
    @RequestMapping(value = "dictDetail", method = RequestMethod.GET)
    public String dictDetail(@RequestParam("id") Long id, Model model) {
        DictDetailEntity dictEntity = detailService.findOne(id).get();
        model.addAttribute(Form.PAGECONTEXT_FORMVALUES, DictDetailConverter.toDTO(dictEntity));
        return "/admin/sys/dict/input-dictDetail-index";
    }

    /**
     * 数据字典新增操作
     *
     * @param dao 表单数据
     * @return
     */
    @RequestMapping(value = "saveOrUpdateDict", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage saveOrUpdate(DictDTO dao) {
        dictService.saveOrUpdate(dao);
        return Returns.success("保存成功");
    }

    /**
     * 数据字典明细新增操作
     *
     * @param dao 表单数据
     * @return
     */
    @RequestMapping(value = "saveOrUpdateDictDetail", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage saveOrUpdateDictDetail(DictDetailDTO dao) {
        detailService.saveOrUpdate(dao);
        return Returns.success("保存成功");
    }
    /**
     * 数据字典删除操作
     *
     * @param id 菜单ID
     * @return
     */
    @RequestMapping(value = "delDictDetail", method = RequestMethod.GET)
    @ResponseBody
    public ResultMessage delDictDetail(@RequestParam("id") Long id) {
        detailService.deleteById(id);
        return Returns.success("删除成功");
    }


    /**
     * 数据字典删除操作
     *
     * @param id 菜单ID
     * @return
     */
    @RequestMapping(value = "delDict", method = RequestMethod.GET)
    @ResponseBody
    public ResultMessage doDelete(@RequestParam("id") Long id) {
        dictService.deleteById(id);
        return Returns.success("删除成功");
    }

    @RequestMapping(value = "getAllDictType", method = RequestMethod.GET)
    @ResponseBody
    public ResultMessage getAllDictType() {
        List<DictEntity> dictEntities = dictService.findAll();
        return Returns.success(DictConverter.toDTO(dictEntities));
    }

}